package cn.itsource.basic.base.service.impl;

import cn.itsource.basic.base.mapper.BaseMapper;
import cn.itsource.basic.base.service.IBaseService;
import cn.itsource.basic.page.PageList;
import cn.itsource.basic.page.Pageable;
import cn.itsource.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.loadOne(id);
    }

    @Override
    @Transactional
    public void saveOrUpdate(T t) {

    }

    @Override
    @Transactional
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public PageList<T> pageQuery(Pageable pageable) {
        Long totals = baseMapper.count(pageable);
        List<T> departments = baseMapper.queryPage(pageable);
        return new PageList<>(totals,departments);
    }

    @Override
    @Transactional
    public void bachDelete(Map<String, String> map) {
        String ids = map.get("ids");
        String[] split = ids.split(",");

        List<Long> list = Arrays.stream(split).map(id -> Long.valueOf(id)).collect(Collectors.toList());
        baseMapper.batchDelete(list);
    }
}
